package handler

import (
	"github.com/gin-gonic/gin"
	"order-pro/model"
	"strconv"
)

func Add(c *gin.Context) {
	good_name := c.PostForm("good_name")
	good_price, _ := strconv.Atoi(c.PostForm("good_price"))
	good_no := c.PostForm("good_no")
	if good_name == "" || good_price == 0 || good_no == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	data := model.Good{
		GoodNum:   good_no,
		GoodName:  good_name,
		GoodPrice: good_price,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "成功",
		})
		return
	}
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var goods []model.Good
	var total int64
	model.DB.Model(&model.Good{}).Count(&total)
	model.DB.Offset(offset).Limit(size).Find(&goods)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  goods,
		"total": total,
	})

}
